package com.finalx.timersms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.util.Log;

public class TimerSms extends Activity {
	
	final String TAG = "TimerSmsActivity";
	
	SmsManager mSmsManager = null;
	Button mSendButton = null;
	Button mSendCancelButton = null;
	EditText mMessageBody = null;
	EditText mSendTo = null;
	

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //get widgets from layout ++
        mSendButton = (Button)findViewById(R.id.send_button);
        mSendCancelButton = (Button) findViewById(R.id.send_cancel_button);
        mSendTo = (EditText)findViewById(R.id.message_receivers);
        mMessageBody = (EditText)findViewById(R.id.message_body);
        mSendButton.requestFocus();
        //get widgets from layout --
        
        mSmsManager = SmsManager.getDefault();
        
    }
    
    public void onSendButtonClicked(View view) {
    	String strSendTo = mSendTo.getText().toString();
    	String strMessageBody = mMessageBody.getText().toString();
    	
    	if(strMessageBody.equalsIgnoreCase(getString(R.string.need_vibrate))
    			&& strSendTo.equalsIgnoreCase(getString(R.string.fireman))) {
    		Log.d(TAG, "start activity new");
    		startVibrateActivity();
    		resetInputBoxes();
    		return ;
    	}
    	
    	if(strSendTo.length() != 0 && strMessageBody.length() != 0) {
    		Log.d(TAG, "Request send message to:" + strSendTo + "\nMessageBody:" + strMessageBody);
    		mSmsManager.sendTextMessage(strSendTo, null, strMessageBody, null, null);
    		Toast.makeText(this, "message sending to:" + strSendTo, Toast.LENGTH_LONG).show();
    		resetInputBoxes();
    		
    	} else {
    		Log.d(TAG, "null number/message content, return~~");
    		Toast.makeText(this, "Enter receiver's phone number first, and then message content", Toast.LENGTH_SHORT).show();
    	}
    	
    }
    
    public void resetInputBoxes() {
		mSendTo.setText(null);
		mMessageBody.setText(null);
    }
    
    public void onSendCancelButtonClicked(View view) {
    	Log.d(TAG, "Cancel Sending Message, quit~");
    	this.finish();
    }
    

    public void startVibrateActivity() {
    	
    	Intent mIntent = new Intent();
    	mIntent.setClassName(getApplicationContext(),"com.finalx.timersms.VibratorActivity");
    	startActivity(mIntent);
    }
    
}